<?php
namespace Tools\Controller;
use Common\Controller\AdminbaseController;

class AdminAboutController extends AdminbaseController{
	
	protected $ab_model;
	
	public function _initialize() {
		parent::_initialize();
		$this->ab_model = M("ToolsAbout");
	}
	
	public function index(){
		$about=M('ToolsAbout')->where("about_id = '1'")->find();
		$about['about_ry_image'] = explode(',', $about['about_ry_image']);
		$about['about_hb_image'] = explode(',', $about['about_hb_image']);
		//print_r($about);
		$this->assign("about",$about);
		$this->display();
	}
	
	public function edit(){
		
		if (IS_POST) {
			if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
				foreach ($_POST['photos_url'] as $key=>$url){
					$photourl=sp_asset_relative_url($url);
					$_POST['about_ry_image'][]=array("url"=>$photourl);
				}
			}
			
			$imgarr = $_POST['photos_url'];
			$_POST['about_ry_image'] = implode(',',$imgarr);
			
			$imgbrr = $_POST['hb_photos_alt'];
			$_POST['about_hb_image'] = implode(',',$imgbrr);
			//print_r($_POST);die();
			
			$id = $_POST['about_id'];
			if ($this->ab_model->create()!==false) {
				if ($this->ab_model->where("about_id = '$id'")->save()!==false) {
					$this->success("保存成功！", U("AdminAbout/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->ad_model->getError());
			}

		}
		
		$about_id = I('about_id');
		$post=$this->ab_model->where("about_id=$about_id")->find();
		
		$post['about_jj_content'] = html_entity_decode($post['about_jj_content']);
		$post['about_sm_content'] = html_entity_decode($post['about_sm_content']);
		
		if($post['about_ry_image'] != ''){
			$rys=explode(',',$post['about_ry_image']);
		}
		if($post['about_hb_image'] != ''){
			$hbs=explode(',',$post['about_hb_image']);
		}
		
		$this -> assign('about_ry_image',$rys);
		$this -> assign('about_hb_image',$hbs);
		
		$this -> assign('about', $post);
		$this->display();
		
	}
	public function show(){
		$about_id = I('about_id');
		$post=$this->ab_model->where("about_id=$about_id")->find();

		$post['about_jj_content'] = html_entity_decode($post['about_jj_content']);
		$post['about_sm_content'] = html_entity_decode($post['about_sm_content']);
		$rys=explode(',',$post['about_ry_image']);
		$hbs=explode(',',$post['about_hb_image']);

		$this -> assign('rys',$rys);
		$this -> assign('hbs',$hbs);
		$this -> assign('about', $post);
		$this->display();
	}
	
	public function contact(){
		$data = M('tools_contact')->order("id asc")->select();
		$this->assign('data',$data);
		$this->display();
	}
	public function addcontact(){
		if(isset($_POST['title'])){
			$add = M('tools_contact')->add($_POST);
			if($add){
				$this->success("添加成功！", U("AdminAbout/contact"));
			} else {
				$this->error("添加失败！");
			}
		}
		$this->display();
	}
	public function editcontact(){
		$id = I('id');
		$data = M('tools_contact')->where("id = '$id'")->find();
		if(isset($_POST['title'])){
			$id = I('id');
			$save = M('tools_contact')->where("id = '$id'")->save($_POST);
			if($save){
				$this->success("修改成功！", U("AdminAbout/contact"));
			} else {
				$this->error("修改失败！");
			}
		}
		$this->assign('data',$data);
		$this->display();
	}
	public function delcontact(){
		$id = I('id');
		$del = M('tools_contact')->where("id = '$id'")->delete();
		if($del){
			$this->success("删除成功！", U("AdminAbout/contact"));
		} else {
			$this->error("删除失败！");
		}
	}
	
}